<?php

namespace app\api\controller;

use app\common\controller\UnLogin;
use app\common\service\SmsService as Sms;
use app\common\service\MailService as Mail;
use app\common\service\UserService as User;
use think\Db;
use \Translate\BaiduApi;
//use JPush\Push;

class Index {

	//接口防刷机制  1分钟10次
	// public function initialize() {

	// 	parent::initialize();
	// 	//ip防刷限制
	// 	// $ip_limit = cache($this->ip.'/index');
	// 	// $time = time();
	// 	// if(!empty($ip_limit)){
	// 	// 	if($ip_limit['num'] > 10){
	// 	// 		$this->retData(1, '接口调用频繁，请一分钟后再试');
	// 	// 	} else {
	// 	// 		$experi = $time-$ip_limit['time'];
	// 	// 		cache($this->ip.'/index',['num'=>$ip_limit['num']+1,'time'=>$ip_limit['time']],$experi);
	// 	// 	}
	// 	// } else {
	// 	// 	cache($this->ip.'/index',['num'=>1,'time'=>$time],60);
	// 	// }
	// }

	public function index(){
		var_dump(BaiduApi::translate('吃饭','en'));die();
		// if (null == false) {
		// 	die('hello,world！');
		// } else {
		// 	die('error');
		// }
		var_dump(date('Y-m-d H:i:s'));

	}
	public function tes($a,$b){

	}
	/*
	图形验证码
	 */
	public function cap() {
		$captcha = new \think\captcha\Captcha();
		$ca = $captcha->entry();
		$code = $ca['code'];
		$im = $ca['src'];
		$time = time();
		ob_start();
		
		// 输出图像
		imagepng($im);
		$content = ob_get_clean();
		$image = base64_encode($content);
		imagedestroy($im);
		// var_dump(base64_encode($content));
		// return response($content, 200, ['Content-Length' => strlen($content)])->contentType('image/png');
		$data['captcha'] = 'data:image/png;base64,' . $image;
		$data['cap_key'] = md5(md5($code) . $time);
		$res = model('CaptchaModel')->setCaptcha($data['cap_key'], strtolower($code));
		if ($res) {
			echo json_encode([
				'code' => 0,
				'msg' => 'success',
				'data' => $data
					], JSON_UNESCAPED_SLASHES);
		} else {
			$this->retData(1, '图形验证码获取失败');
		}
	}


	//邀请码是否必填
	public function isinvite(){
		$data = C('register_is_invite_code');
		if($data == 0){
			$this->retData(0,'邀请码选填',1);
		} else if ($data == 1) {
			$this->retData(0,'邀请码必填',2);
		}
	}

	
	/*
	短信接口
	*/
	public function sms() {
		$quhao = P('quhao') ?: '91';
		$phone = P('phone')?:'9179920619';
		$type = P('type') ?: 1;
		if (empty($phone)) {
			$this->retData(1, '缺少参数');
		}
		$type = 1;
		$res = Sms::sms($quhao, $phone, $type);
		if ($res['code'] == 0) {
			$this->retData(0, '发送成功');
		} else {
			$this->retData(1, $res['msg']);
		}
	}


	/*
	邮箱接口
	*/
	public function mail() {
		$mail = P('mail')?:'';
		$type = P('type') ?: 1;
		if (empty($mail)) {
			$this->retData(1, '缺少参数');
		};
		$type = 1;
		$res = Mail::mail($mail, $type);
		if ($res['code'] == 0) {
			$this->retData(0, '发送成功');
		} else {
			$this->retData(1, $res['msg']);
		}
	}


	/*
	注册赠送
	 */
	public function regReward(){
		$data = Db::table('tp_reg_config')->alias('a')
						->join('tp_coin_wallet w','w.id=a.wid')
						->field('w.name,a.amount,a.is_open')
						->find();
		$this->retData(0,'success',$data);
	}

	/*
	获取下载地址
	 */
	public function getDownLoad(){
		$data = [];
		$data['download_url_az'] = C('download_url_az');
		$data['download_url_ios'] = C('download_url_ios');
		$data['download_url_google'] = C('download_url_google');
		$this->retData(0,'success',$data);
	}

	/*
	验证token
	 */
	public function checkToken(){
		$uid   = P('uid');
        $token = P('token');
        if (empty($uid) || empty($token)) {
            $this->retData(1, '缺少参数');
        }
        $check_token = model('TokenModel')->veriToken($uid, $token);
        if(empty($check_token)){
            $this->retData(1, '该钱包已被导走');
        } else {
        	$this->retData(0, 'success');
        }
	}
	
	public function kfAccount(){
		$uid = '10072';
		if(empty($uid)){
			$this->retData(1,'缺少参数用户ID(uid)');
		}
		$data = [];
		$aaa = Db::table('tp_user_team')->where('x_uid',$uid)->column('uid');
// 		$admin = Db::table('tp_admin')->order('data_uid desc')->column('data_uid');
// 		$pid = $this->user_info['pid'];
// 		$data_uid = 0;
		$data_uid = Db::table('tp_admin')->where('data_uid','in',$aaa)->order('data_uid desc')->value('data_uid');
// 		while ($pid) {
// 			if (in_array($pid, $admin)) {
// 				$data_uid = $pid;
// 				break;
// 			} else {
// 				$pid = Db::table('tp_user')->where('id',$pid)->value('pid');
// 			}
// 		}
		if ($data_uid) {
			$data['kf1'] = Db::table('tp_admin')->where('data_uid',$data_uid)->value('kf1');
			$data['kf2'] = Db::table('tp_admin')->where('data_uid',$data_uid)->value('kf2');
			if (!$data['kf1']) {
				$data['kf1'] = C('kf_code1');
			}
			if (!$data['kf2']) {
				$data['kf2'] = C('kf_code2');
			}
		} else {
			$data['kf1'] = C('kf_code1');
			$data['kf2'] = C('kf_code2');
		}
		
		// $data['kf2'] = explode(',', $data['kf2']);
		$this->retData(0,'success',$data);
	}
}